/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.base.util;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 *
 * @author Administrator
 */
public class ReflectUtil {

    public static <T> T newObject(Class<T> clazz) {
        try {
            T t = clazz.newInstance();
            return t;
        } catch (Exception ex) {
            throw new IllegalArgumentException(ex);
        }
    }
    
    public static Object invoke(Object object, String methodName, Class[] parameters, Object[] parameterValues) {
        try {
            Class objectClass = object.getClass();
            Method method = objectClass.getMethod(methodName, parameters);
            return method.invoke(object, parameterValues);
        } catch (Exception ex) {
            throw new IllegalArgumentException(ex);
        }
    }

    public static Object set(Object object, String name, Object value) {
        try {
            StringBuilder sb = new StringBuilder(name);
            Character c = sb.charAt(0);
            sb.setCharAt(0, Character.toUpperCase(c));
            String methodName = "set" + sb.toString();
            Class objectClass = object.getClass();
            Class parameterClass = null;
            if (value == null) {
                Field field = objectClass.getField(name);
                parameterClass = field.getDeclaringClass();
            } else {
                parameterClass = value.getClass();
            }
            return invoke(object, methodName, new Class[]{parameterClass}, new Object[]{value});
        } catch (Exception ex) {
            throw new IllegalArgumentException(ex);
        }
    }
    public static Exists<Field> getFieldValue(Class clazz, String name,
			Class annotation) {
		Exists<Field> exists = new Exists<Field>();
		exists.setExists(false);
		exists.setObject(null);
		try {
			if (clazz == null) {
				return exists;
			}
			Field field = clazz.getDeclaredField(name);
			if (field == null) {
				return exists;
			}
			Annotation anntion = field.getAnnotation(annotation);
			if (annotation != null) {
				field.setAccessible(true);
				exists.setExists(true);
				exists.setObject(field);
				return exists;
			} else {
				return exists;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			 e.printStackTrace();
			
			return exists;
		}
	}
    public static Exists<Object> getFieldValue(Object object, String name,
			Class annotation) {
		Exists<Object> exists = new Exists<Object>();
		exists.setExists(false);
		exists.setObject(null);
		try {
			if (object == null) {
				return exists;
			}
			Class clazz = object.getClass();
			Field field = clazz.getDeclaredField(name);
			if (field == null) {
				return exists;
			}
			Annotation anntion = field.getAnnotation(annotation);
			if (annotation != null) {
				field.setAccessible(true);
				Object value = field.get(object);
				exists.setExists(true);
				exists.setObject(value);
				return exists;
			} else {
				return exists;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			return exists;
		}
	}

}
